﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Pigs.Enemies
{
    class Basic_Pig : pigs.Pig
    {
        public Texture2D basicPigTexture;

        // Animation variables
        int frameCount = 0; // Current frame
        int frameSkipX = 76; // Width of a frame
        int animationCount; // How many ticks since the last frame change.
        int animationMax; // How many ticks to change frame after. 
        Rectangle source;

        /// <summary>
        /// make the new pig
        /// </summary>
        /// <param name="texture">texture to use</param>
        /// <param name="position">where should the pig start out</param>
        public Basic_Pig(Texture2D texture, Vector2 position, int numPlayers)
            :base(texture, position)
        {
            START_HEALTH = 1;
            START_DAMAGE = 8;
            START_SPEED = 2.5f;
            VALUE = 100;

            this.shouldEatSlop = true;
            this.basicPigTexture = texture;
            this.position = position;
            this.active = true; // We initialize the enemy to be active so it will be update in the game

            if (numPlayers == 1) { this.health = START_HEALTH; }
            else if (numPlayers > 1 && numPlayers < 3) { this.health = START_HEALTH * 2; }
            else {this.health = this.START_HEALTH * 3; }
            
            if (numPlayers == 1) { this.damage = this.START_DAMAGE; }
            else if (numPlayers > 2 && numPlayers < 4) { this.damage = this.START_DAMAGE * 2; }
            else { this.damage = this.START_DAMAGE * 3; }
            
            this.speed = START_SPEED;
            this.vertSpeed = 0;
            this.value = VALUE;
            this.effect = SpriteEffects.None;
            this.ID = "basic";
            this.lead = false;
            this.buff = false;
            this.escaped = false;
            this.hitByFlame = false;
            this.ghost = false;

            // Start with frame 1 of image array
            this.frameCount = 1;
            this.animationCount = 0;
            this.animationMax = 3;

            this.source = new Rectangle(this.frameSkipX * this.frameCount, 0, this.frameSkipX, this.texture.Height);
            
            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);

            //get the color data
            colorMap = new Color[source.Width * source.Height];
            this.texture.GetData(0, source, colorMap, 0, source.Width * source.Height);
        }


        public void doAI(SteelPig p, bool SteelPig, List<pigs.Pig> pigs, List<pigs.Scenery> obstacles)
        {
            if (this.lead && !this.buff)
            {
                this.health *= 3;
                this.damage *= 3;
                this.buff = true;
            }
            bool foundSuicide = false;
            // Iterate through all the other pigs, and find any nearby suicide pigs.
            foreach (pigs.Pig q in pigs)
            {
                // As long as you haven't found a suicide pig in range yet, keep checking
                if (!foundSuicide)
                {
                    if (q.ID.Equals("suicide"))
                    {
                        // Find the distance between two pigs.
                        float dist = Vector2.Distance(this.position, q.position);

                        // If a suicide pig is within a distance of 150, then move up or down and try to get out of the suicide pig's
                        // death range
                        if (dist < 150)
                        {
                            foundSuicide = true;
                            //If the calling pig has a position Y that is greater than the others, move it in that direction
                            //Otherwise move it in the other y directinon
                            if (this.position.Y > q.position.Y)
                            {
                                if(this.ObstacleMove(obstacles, "down", this.position).Equals("none"))
                                {
                                    this.position.Y += 5;
                                }
                            }
                            else
                            {
                                if (this.ObstacleMove(obstacles, "up", this.position).Equals("none"))
                                {
                                    this.position.Y -= 5;
                                }
                            }
                        }
                    }
                }
            }

            // If a suicide pig is within range, it will override the desire to hide behind an active steel pig
            if (!foundSuicide)
            {
                // If a steel pig is active on the screen
                if (SteelPig)
                {
                    this.followSteel(p);
                }
                // Make sure that the if a steel pig becomes inactive, that the other basic pigs will return
                // to their slop eating methods
                else
                {
                    this.vertSpeed = 0;
                    this.shouldEatSlop = true;
                    if (this.speed < START_SPEED && ghost)
                    {
                        this.speed += 0.1f;
                    }
                    this.gohome(obstacles);
                }
            }

            // Move the pig
            this.position.X -= this.speed;

            if (health <= 0)
            {
                // By setting the Active flag to false, the game will remove this object from the 
                // active game list
                active = false;
            }
        }

        public override void basicUpdate(GameTime gameTime, pigs.Pig p, bool steelPig, List<pigs.Pig> pigs, List<pigs.Scenery> obstacles)
        {
            if (this.shouldGhost && this.color.A > 70)
            {
                this.color.A--;
                this.color.A--;
                this.color.A--;
            }

            // Overwrite any basic pig AI by saying "If I can see a mountain ___ distance ahead of me, slow down and move up"
            if (!avoidScenery(obstacles))
            {
                this.doAI((SteelPig)p, steelPig, pigs, obstacles);
            }
            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);
            UpdateAnimation();
        }

        // Perform the animation updates on the image
        public void UpdateAnimation()
        {
            this.animationCount += 1;
            if (this.animationCount > this.animationMax)
            {
                this.animationCount = 0;
                this.frameCount += 1;
                if (this.frameCount == 5)
                {
                    this.frameCount = 0;
                }
            }
        }
        
        //Draw the updated texture image
        public override void Draw(SpriteBatch spriteBatch)
        {
            // Update the source rectangle, based on where in the animation we are.  
            this.source.X = this.frameSkipX * this.frameCount;

            spriteBatch.Draw(texture, position, this.source, color, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
        }
    }
}
